﻿//========================================================= 
/**@file ParamHelper.h
 * @brief 参数解析辅助工具 
 * 
 * @date 2015-06-10   20:18:12
 * @author Zhyioo 
 * @version 1.0
 */ 
//--------------------------------------------------------- 
#ifndef _LIBZHOUYB_PARAMHELPER_H_
#define _LIBZHOUYB_PARAMHELPER_H_
//--------------------------------------------------------- 
#include "../../include/Extension.h"
//--------------------------------------------------------- 
namespace zhou_yb {
namespace application {
namespace tools {
//--------------------------------------------------------- 
/// 参数解析 
class ParamHelper
{
protected:
    ParamHelper() {}
public:
    /**
     * @brief 解析串口号
     * 
     * @param [in] strCOM 输入的串口号 
     * 
     * - 格式:
     *  - NULL = AUTO
     *  - "" = AUTO
     *  - "1" 数字形式 
     *  - "COM1" 带COM开头 
     *  - "AUTO" 自动枚举第一个串口
     * .
     * 
     * @return uint 获取到的串口号 
     */
    static uint ParseCOM(const char* strCOM)
    {
        uint port = 0;
        ByteArray strArray(strCOM);

        if(!strArray.IsEmpty())
        {
            if(StringConvert::StartWith(strArray, "COM", true))
            {
                port = ArgConvert::FromString<uint>(strCOM + 3);
            }
            else if(StringConvert::Compare(strArray, "AUTO", true))
            {
                // 需要自动枚举第一个能打开的端口  
                #ifdef _WIN32
                ComDevice dev;
                list_t<uint> comlist;
                dev.EnumDevice(comlist);

                if(comlist.size() < 1)
                    return false;

                port = comlist.front();
                #else
                // 查找 "/dev/ttyS" 的设备
                list_t<string> ttylist;
                FolderHelper::EnumFiles("/dev", ttylist);

                list_t<string>::iterator ttyItr;
                for(ttyItr = ttylist.begin();ttyItr != ttylist.end(); ++ttyItr)
                {
                    ByteArray ttyName(ttyItr->c_str(), ttyItr->length());
                    if(StringConvert::StartWith(ttyName, "ttyS", true))
                    {
                        port = ArgConvert::FromString<uint>(ttyItr->c_str() + 4);
                        break;
                    }
                }
                #endif
            }
            else if(_is_digital(strCOM[0]))
            {
                port = ArgConvert::FromString<uint>(strCOM);
            }
        }

        return port;
    }
    /// 解析串口号 "COM1:B,115200" "COM1,115200"
    static uint ParseCOM(const char* strCOM, byte& gate, uint& baud)
    {
        ByteArray comArray(strCOM);
        size_t indexGate = StringConvert::IndexOf(comArray, ':');
        size_t indexBaud = StringConvert::IndexOf(comArray, ',');

        ByteBuilder tmp(4);
        if(indexBaud > indexGate)
        {
            tmp = comArray.SubArray(indexGate + 1, indexBaud - indexGate - 1);
            if(!tmp.IsEmpty()) gate = _get_upper(tmp[0]);
        }
        if(indexBaud != SIZE_EOF)
        {
            tmp = comArray.SubArray(indexBaud + 1);
            if(!tmp.IsEmpty()) baud = ArgConvert::FromString<uint>(tmp.GetString());
        }
        
        tmp = comArray.SubArray(0, indexGate);
        return ParseCOM(tmp.GetString());
    }
    /**
     * @brief 将精确到小数点后两位的金额转换为以分为单位的金额 
     * 
     * @param [in] sMoney 需要转换的金额 "0.13"
     * @param [in] degree [default:2] 小数点后面的精度 
     * 
     * @return uint 转换后以分为单位的金额 "0.13" => "13"
     */
    static uint ParseMoney(const char* sMoney, uint degree = 2)
    {
        uint money = 0;
        uint deg = 1;
        // 整数部分 
        size_t index = StringConvert::IndexOf(sMoney, '.');
        // 获取整数部分 
        ArgConvert::FromString<uint>(sMoney, money);
        for(uint i = 0;i < degree; ++i)
            deg *= 10;
        money *= deg;
        
        if(index != SIZE_EOF)
        {
            uint tmp = 0;
            ByteBuilder tmpBuff(degree);
            tmpBuff = sMoney + index + 1;
            ByteConvert::Fill(tmpBuff, degree, true);
            ArgConvert::FromString<uint>(tmpBuff.GetString(), tmp);

            tmp %= deg;
            money += tmp;
        }

        return money;
    }
};
//--------------------------------------------------------- 
} // namespace tools
} // namespace application
} // namespace zhou_yb
//--------------------------------------------------------- 
#endif // _LIBZHOUYB_PARAMHELPER_H_
//========================================================= 